unity subtract class

50

unity subtract class -

//Short Reference
public myClass{
public static myClass operator -(myClass instanceA, myClass instanceB){
//Do custom operation behaviour
return desiredValue;
}
}


//Example:
//To apply mathematical operators to instances of custom classes
//You need to do "Operator Overloading"
public Length{
//Our Length class is simply a length in inches and feet
public int feet;
public int inches;
//Constructor
public Length(int Feet, int Inches){
this.feet = Feet;
this.inches = Inches;
}
//we can declare our operator like a function, and identify it as an operator
//pass in 
public static Length operator +(Length l1, Length l2){
//for this example, we'll use a third instance to store the return data
Length l3 = new Length();
//subtract the feet and inches...
l3.inches = l1.inches-l2.inches;
l3.feet = l1.feet-l2.feet;
//and if the inches are negative, subtract from the feet...
if(l3.inches<0){
l3.feet--;
l3.inches += 12;
}
//then return it, just like a function
return l3;
}

}

Comments

Submit
0 Comments